/*******************************************************************************
 * Copyright 2011 Mile Orešković
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package com.mileoresko.hierarchism;

import java.util.Enumeration;
import java.util.Hashtable;

public abstract class HSMBareState {

    protected String label;
    protected HierarchicalStateMachine owningStateMachine;

    private Hashtable nestedHsms;
    private Hashtable runtimeNestedHsms;

    protected HSMBareState(final HierarchicalStateMachine owningStateMachine, final String label) {
        this.label = label;
        this.owningStateMachine = owningStateMachine;
        defineNestedHsms();
    }

    protected final void addNestedHsm(final HierarchicalStateMachine hsm) {
        if (nestedHsms == null) {
            nestedHsms = new Hashtable();
        }
        nestedHsms.put(new Integer(hsm.hashCode()), hsm);
    }

    public synchronized final void addRuntimeNestedHsm(Object hsmId, final HierarchicalStateMachine hsm) {
        if (runtimeNestedHsms == null) {
            runtimeNestedHsms = new Hashtable();
        }
        runtimeNestedHsms.put(hsmId, hsm);
    }

    public synchronized final void removeRuntimeNestedHsm(Object hsmId) {
        if (runtimeNestedHsms != null) {
            runtimeNestedHsms.remove(hsmId);
        }
    }

    protected abstract void defineNestedHsms();

    protected abstract void entryAction(final Object arg);

    protected abstract void exitAction(final Object arg);

    protected abstract boolean isTerminal();

    synchronized final void dispatchEvent(final HSMEvent event, final Object arg) {
        if (nestedHsms != null) {
            final Enumeration values = nestedHsms.elements();
            while (values.hasMoreElements()) {
                ((HierarchicalStateMachine) values.nextElement()).pushEvent(event, arg);
            }
        }
        if (runtimeNestedHsms != null) {
            final Enumeration values = runtimeNestedHsms.elements();
            if (values != null) {
                while (values.hasMoreElements()) {
                    ((HierarchicalStateMachine) values.nextElement()).pushEvent(event, arg);
                }
            }
        }
    }

    synchronized final void entryActionInternal(final Object arg) {
        entryAction(arg);
        if (nestedHsms != null) {
            final Enumeration values = nestedHsms.elements();
            while (values.hasMoreElements()) {
                ((HierarchicalStateMachine) values.nextElement()).start(arg);
            }
        }
    }

    synchronized final void exitActionInternal(final Object arg) {
        if (nestedHsms != null) {
            final Enumeration values = nestedHsms.elements();
            while (values.hasMoreElements()) {
                ((HierarchicalStateMachine) values.nextElement()).halt(arg);
            }
        }
        if (runtimeNestedHsms != null) {
            final Enumeration values = runtimeNestedHsms.elements();
            while (values.hasMoreElements()) {
                ((HierarchicalStateMachine) values.nextElement()).halt(arg);
            }
            runtimeNestedHsms.clear();
            runtimeNestedHsms = null;
        }
        exitAction(arg);
    }

    public String toString() {
        return label;
    }

    public synchronized HierarchicalStateMachine getRuntimeNestedHsm(Object hsmId) {
        if (runtimeNestedHsms != null) {
            return (HierarchicalStateMachine) runtimeNestedHsms.get(hsmId);
        } else {
            return null;
        }
    }

    public synchronized Hashtable getRuntimeNestedHsms() {
        return runtimeNestedHsms;
    }
}
